<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="activeName" type="border-card">
        <el-tab-pane label="推荐职位" name="1">
          <el-form
            ref="form"
            :model="weightValueForm"
            label-width="80px"
            style="margin-top: 20px"
          >
            <el-form-item label="">
              <el-table :data="recommendPosition" style="width: 100%" border>
                <el-table-column
                  prop="weightName"
                  label="权重项"
                  width="width"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="weightValue"
                  label="权重值"
                  width="width"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-input-number
                      v-model="scope.row.weightValue"
                      @change="handleWeightChange(scope.row)"
                      label="描述文字"
                      >{{ scope.row.weightValue }}</el-input-number
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" @click="saveClick">保存</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="推荐简历" name="2">
          <el-form
            ref="form"
            :model="weightJlValueForm"
            label-width="80px"
            style="margin-top: 20px"
          >
            <el-form-item label="">
              <el-table
                :data="recommendPositionResume"
                style="width: 100%"
                border
              >
                <el-table-column
                  prop="weightName"
                  label="权重项"
                  width="width"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="weightValue"
                  label="权重值"
                  width="width"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-input-number
                      v-model="scope.row.weightValue"
                      @change="handleChange(scope.row)"
                      label="描述文字"
                      >{{ scope.row.weightValue }}</el-input-number
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" @click="recommendJlClick"
                >保存</el-button
              >
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { selWeightListApi, updateWeightByIdsApi } from "@/api/system";
export default {
  data() {
    return {
      activeName: "1",
      type1: 1,
      type2: 2,
      recommendPosition: [],
      recommendPositionResume: [],
      // 推荐职位权重值参数
      // weightValueForm: {
      //   id: null,
      //   type: 1,
      //   weightName: null,
      //   weightValue: null,
      // },
      weightValueForm: [],
      // 推荐简历权重值参数
      weightJlValueForm: [],
    };
  },
  watch: {
    activeName(newVal, oldVal) {
      console.log(newVal);
      if (newVal === "1") {
        this.getJobWeightData();
      } else {
        this.getResumeWeightData();
      }
    },
  },
  created() {
    this.getJobWeightData();
  },
  methods: {
    // 推荐简历
    async recommendJlClick() {
      // 检查是否有修改过的权重值
      const hasWeightChanges = this.weightJlValueForm.length > 0;

      // 根据是否有修改来决定传递哪个数据
      const dataToSave = hasWeightChanges
        ? this.weightJlValueForm
        : this.recommendPositionResume;

      try {
        const res = await updateWeightByIdsApi(dataToSave);
        console.log(res);
        if (res.code === 1) {
          this.$message.success("保存成功!");
          // 保存成功后清空修改记录
          this.weightJlValueForm = [];
          // 重新获取数据
          this.getResumeWeightData();
        } else {
          this.$message.error("保存失败");
        }
      } catch {
        this.$message.error("保存失败");
      }
    },
    // 推荐职位保存
    async saveClick() {
      // 检查是否有修改过的权重值
      const hasWeightChanges = this.weightValueForm.length > 0;

      // 根据是否有修改来决定传递哪个数据
      const dataToSave = hasWeightChanges
        ? this.weightValueForm
        : this.recommendPosition;

      try {
        const res = await updateWeightByIdsApi(dataToSave);
        console.log(res);
        if (res.code === 1) {
          this.$message.success("保存成功!");
          // 保存成功后清空修改记录
          this.weightValueForm = [];
          // 重新获取数据
          this.getJobWeightData();
        } else {
          this.$message.error("保存失败");
        }
      } catch {
        this.$message.error("保存失败");
      }
    },
    // 推荐简历修改权重值
    handleChange(row) {
      console.log(row);
      console.log(this.weightJlValueForm);
      // 检查是否已经存在相同id的记录
      const index = this.weightJlValueForm.findIndex(
        (item) => item.id === row.id
      );
      console.log(index);
      if (index > -1) {
        // 如果存在，更新该记录
        this.weightJlValueForm[index] = row;
      } else {
        // 如果不存在，添加新记录
        this.weightJlValueForm.push(row);
      }
      console.log("修改后的简历权重值列表：", this.weightJlValueForm);
    },
    // 权重值修改
    handleWeightChange(row) {
      // 检查是否已经存在相同id的记录
      const index = this.weightValueForm.findIndex(
        (item) => item.id === row.id
      );
      if (index > -1) {
        // 如果存在，更新该记录
        this.weightValueForm[index] = row;
      } else {
        // 如果不存在，添加新记录
        this.weightValueForm.push(row);
      }
      console.log("修改后的权重值列表：", this.weightValueForm);
    },
    // 推荐职位
    async getJobWeightData() {
      try {
        const res = await selWeightListApi(this.type1);
        console.log("获取到的默认数据：", res);
        this.recommendPosition = res.data;
        // 重置修改记录
        this.weightValueForm = [];
      } catch {}
    },
    // 推荐简历
    async getResumeWeightData() {
      try {
        const res = await selWeightListApi(this.type2);
        console.log("获取到的简历默认数据：", res);
        this.recommendPositionResume = res.data;
        // 重置修改记录
        this.weightJlValueForm = [];
      } catch {}
    },
  },
};
</script>

<style scoped></style>
